<?php
declare (strict_types=1);

namespace app\admin\model\site\navi;

use app\common\model\BaseModel;
use app\school\model\school\School;
use think\facade\Config;

/**
 * 定义头部导航数据模型
 */
class NaviTop extends BaseModel
{
    /**
     * 表名
     *
     * @var string
     */
    protected $name = 'site_navi_top';

    /**
     * 设置自动写入时间
     *
     * @var boolean
     */
    protected $autoWriteTimestamp = true;

    /**
     * 时间显示格式
     *
     * @var string
     */
    protected $dateFormat = 'Y-m-d H:i';

    /**
     * 定义表主键
     *
     * @var string
     */
    protected $pk = 'id';

    protected $append = ['data_type_text', 'content_text']; // 全局查询范围
    protected $globalScope = ['mhm'];

    /**
     * 新增数据事件
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-03-14
     *
     * @return   string
     */
    public static function onBeforeInsert($navi)
    {
        // 机构
        $navi->mhm_id = $navi->getAttr('mhm_id') ?: request()->mhm_id;
    }

    /**
     * 机构
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-02
     *
     * @return   [type]                         [description]
     */
    public function school()
    {
        return $this->belongsTo(School::class, 'mhm_id')->bind(['school_name' => 'title']);
    }

    /**
     * 限制机构条件
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-16
     *
     * @param    [type]                         $query [description]
     * @return   [type]                                [description]
     */
    public function scopeMhm($query)
    {
        // 机构
        $query->where('mhm_id', request()->mhm_id);
    }

    /**
     * 获取内容文字
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-05
     *
     * @param    [type]                         $value [description]
     * @param    [type]                         $data  [description]
     * @return   [type]                                [description]
     */
    public function getContentTextAttr($value, $data)
    {
        $content = '';

        switch ($data['data_type']) {
            case 0:
                // 协议
                $content = '不跳转';
                break;
            case 1:
                // 资源按关键字跳转
                $list    = $this->getContentList();
                $list    = array_column($list, 'title', 'value');
                $content = $list[$data['content']] ?? '未知';
                break;
            default:
                // 其他
                $content = $data['content'];
                break;
        }

        return $content;
    }

    /**
     * 获取数据类型文字
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-05
     *
     * @param    [type]                         $value [description]
     * @param    [type]                         $data  [description]
     * @return   [type]                                [description]
     */
    public function getDataTypeTextAttr($value, $data)
    {
        $text = $this->getDataTypeInfo();

        return $text[$data['data_type']] ?? '-';
    }

    public function getDataTypeInfo()
    {
        return [
            0 => '不跳转',
            1 => '指定内容',
            2 => '链接',
        ];
    }

    /**
     * 获取数据类型选项数据
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-04
     *
     * @return   [type]                         [description]
     */
    public function getDataTypeOptions()
    {
        $options = [];

        $status = $this->getDataTypeInfo();
        foreach ($status as $key => $value) {
            $options[] = ['title' => $value, 'value' => $key];
        }

        return $options;
    }

    /**
     * 获取跳转内容选项数据
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-29
     *
     * @return   [type]                         [description]
     */
    public function getContentOptions()
    {
        // 开发的应用
        $open_app = Config::get('eduline.appinfo.open_app');
        $need     = ['school', 'live', 'video', 'classes', 'topic'];
        $groups   = [
            'homepage' => '首页',
            'teacher'  => '讲师',
            'school'   => '机构',
            'course'   => '课程',
            'topic'    => '资讯',
            'exam'     => '考试',
            'sns'      => '互动'
        ];
        $list     = $this->getContentList();
        $data     = [];

        foreach ($list as $options) {
            // 是否允许选择
            if (in_array($options['value'], $need) && !in_array($options['value'], $open_app)) {
                continue;
            }

            $group = $options['group'];
            if (!isset($data[$group])) {
                $data[$group] = [
                    'title'   => $groups[$group],
                    'options' => [],
                ];
            }

            $data[$group]['options'][] = $options;
        }

        return array_values($data);
    }

    protected function getContentList()
    {
        $list = [
            ['title' => '首页', 'value' => 'homepage', 'group' => 'homepage'],
            ['title' => '讲师列表', 'value' => 'teacher', 'group' => 'teacher'],
            ['title' => '课程列表', 'value' => 'course', 'group' => 'course'],
            ['title' => '点播课程列表', 'value' => 'video', 'group' => 'course'],
            ['title' => '直播课程列表', 'value' => 'live', 'group' => 'course'],
            ['title' => '班级课课程列表', 'value' => 'classes', 'group' => 'course'],
            ['title' => '资讯列表', 'value' => 'topic', 'group' => 'topic'],
            ['title' => '考试列表', 'value' => 'exam', 'group' => 'exam'],
            ['title' => '学友圈', 'value' => 'circle', 'group' => 'sns'],
        ];
        if (request()->is_platform) {
            $list[] = [
                'title' => '机构列表',
                'value' => 'school',
                'group' => 'school',
            ];
        }

        return $list;
    }

}
